package kr.co.mustgo.model.service.review;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import kr.co.mustgo.domain.review.Comment;
import kr.co.mustgo.domain.review.ReviewsBoard;
import kr.co.mustgo.model.dao.review.ReviewsDAO;
import kr.co.mustgo.util.BoardUtilities;
import kr.co.mustgo.util.PagingBean;

@Service
public class ReviewsServiceImpl implements ReviewsService{

	@Autowired
	public ReviewsDAO reviewsDAO;

	//글 입력을 처리하는 메소드
	@Override
	public void writePost(ReviewsBoard board) {
		//String content = BoardUtilities.changeContentForDB(board.getContent());
		//board.setContent(content);
		reviewsDAO.insertPostForNewCount(board);
	}

	//하나의 게시물을 조회하는 메소드
	@Override
	public ReviewsBoard getPostByNo(int no) {
		reviewsDAO.updateViewCount(no);
		ReviewsBoard board = reviewsDAO.selectPostByNo(no);
		List<Comment> comment = reviewsDAO.selectCommentByNo(no);
		if(comment != null){
			board.setComment(comment);
		}
		return board;
	}

	//답변 폼이나 수정폼을 위해 하나의 게시물을 조회하는 메소드(조회수는 증가시키지 않는다.)
	@Override
	public ReviewsBoard getPostByNoForForm(int no) {
		ReviewsBoard board = reviewsDAO.selectPostByNo(no);
		
		//board.setContent(BoardUtilities.changeContentForTextArea(board.getContent()));
		
		return board;
	}

	//게시물을 수정하는 메소드
	@Override
	public void updatePost(ReviewsBoard board) {
		//board.setContent(BoardUtilities.changeContentForDB(board.getContent()));
		reviewsDAO.updatePost(board);
	}

	//하나의 게시물을 삭제하는 메소드
	@Override
	public void deletePost(int no) {
		reviewsDAO.deletePostByNo(no);
	}

	//페이징 처리 목록을 조회하는 메소드
	//조회한 게시물을 List에 넣고 페이징 처리를 위한 PagingBean객체를 map에 넣어서 return 해준다.
	@Override
	public Map getReviewsBoardList(int page) {
		HashMap map = new HashMap();
		
		List list = reviewsDAO.selectReviewsBoardList(page);
		map.put("list", list);
		
		int total = reviewsDAO.selectTotalPost();
		PagingBean paging = new PagingBean(total, page);
		map.put("paging", paging);
		
		return map;
	}

	@Override
	public void writeComment(Comment comment) {
		reviewsDAO.insertComment(comment);
	}

	@Override
	public void updateComment(Comment comment) {
		reviewsDAO.updateComment(comment);
	}

	@Override
	public void deleteComment(int cmNo) {
		reviewsDAO.deleteComment(cmNo);
	}

	@Override
	public void deleteCommentByNo(int no) {
		reviewsDAO.deleteCommentByNo(no);
	}

	//댓글 하나 조회
	@Override
	public Comment getComment(int cmNo) {
		return reviewsDAO.selectCommentBycmNo(cmNo);
	}

	//원본글에 포함된 댓글들 조회
	@Override
	public List<Comment> getCommentByNo(int no) {
		return reviewsDAO.selectCommentByNo(no);
	}

	@Override
	public Map getSearchList(int page, int category,
			String keyword) {
		HashMap map = new HashMap();	//리턴 해줄 맵(조회 해존 값과 페이지를 넣는다.)
		
		String search = null;
		String search2 = "";
		int searchBy = 0;
		if(category == 0){
			search = "title";
			searchBy = 0;
		}else if(category == 1){
			search = "writer";
			searchBy = 1;
		}else{
			search = "title";
			search2 = "content";
			searchBy = 2;
		}
		//조회에 필요한 값들 셋팅
		map.put("page", page);
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("search", search);
		map.put("search2", search2);
		map.put("keyword", keyword);
		map.put("searchBy", searchBy);
		
		//컨트롤러에 넘겨줄 map 셋팅
		int total = reviewsDAO.selectTotalSearch(map);
		List<ReviewsBoard> list = reviewsDAO.selectSearchList(map);
		PagingBean paging = new PagingBean(total, page);
		map.put("list", list);
		map.put("paging", paging);
		return map;
	}
	
}
